package com.borqsprocessmanager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import android.content.ContentValues;
import android.os.Parcel;
import android.os.Parcelable;

import com.borqsprocessmanager.db.DBProvider.ActionTable;
import com.borqsprocessmanager.info.ProcessStatsInfo;
import com.borqsprocessmanager.info.ReceiverProcessInfo;
import com.borqsprocessmanager.util.Log;

public class BatteryManager implements Parcelable {
    final String TAG = "BatteryManager";

    // private BatteryManagerService mService;
    public HashMap<String, ReceiverProcessInfo> mActionMap;

    public BatteryManager(HashMap<String, ReceiverProcessInfo> actionMap) {
        this.mActionMap = actionMap;
    }

    // public void saveDisableMapForDB(
    // HashMap<String, ReceiverProcessInfo> mDisableMap,
    // HashMap<String, ReceiverProcessInfo> mEnableMap) {
    // ArrayList<String> deleteKeyLists = new ArrayList<String>();
    // ArrayList<ContentValues> addLists = new ArrayList<ContentValues>();
    //
    // HashMap<String, ReceiverProcessInfo> curAction = mService.actionMap;
    // Set<String> dSet = mDisableMap.keySet();
    // Set<String> eSet = mEnableMap.keySet();
    //
    // for (String key : eSet) {
    // curAction.remove(key);
    // deleteKeyLists.add(key);
    // Log.v(TAG, "delete_AppName=" + key);
    // }
    // for (String key : dSet) {
    // curAction.put(key, mDisableMap.get(key));
    // ContentValues cv = new ContentValues();
    // ReceiverProcessInfo info = mDisableMap.get(key);
    // cv.put(ActionTable.PROCESS_NAME, info.processName);
    // cv.put(ActionTable.APPNAME, info.appName);
    // cv.put(ActionTable.PKG_NAME, info.packageName);
    // cv.put(ActionTable.ACATION_TYPE, info.isClose);
    // addLists.add(cv);
    // }
    // if (addLists.size() > 0) {
    // ContentValues[] values = new ContentValues[addLists.size()];
    // addLists.toArray(values);
    // App.mContext.getContentResolver().bulkInsert(
    // ActionTable.CONTENT_URI, values);
    // Log.v(TAG, "addLists.size() >0===" + values.length);
    // }
    // if (deleteKeyLists.size() > 0) {
    // String[] selectionArgs = new String[deleteKeyLists.size()];
    // deleteKeyLists.toArray(selectionArgs);
    // for (String processname : deleteKeyLists) {
    // App.mContext.getContentResolver().delete(
    // ActionTable.CONTENT_URI,
    // ActionTable.PROCESS_NAME + "=?",
    // new String[] { processname });
    // }
    // Log.v(TAG, "deleteLists.size() >0===" + selectionArgs.length);
    // }
    // }

    public void saveDisableMapForDB(boolean isRooit,
            HashMap<String, ReceiverProcessInfo> autoStartMap,
            HashMap<String, ReceiverProcessInfo> mDisableMap) {
        ArrayList<ContentValues> addLists = new ArrayList<ContentValues>();
        // disable
        // if (isRooit) {
        // HandleProcess(getAutoStartAppMap(autoStartMap, mDisableMap), false);
        // }

        HashMap<String, ReceiverProcessInfo> curAction = mActionMap;
        Set<String> dSet = mDisableMap.keySet();

        for (String key : dSet) {
            curAction.put(key, mDisableMap.get(key));// 存入内存acitonMap
            ContentValues cv = new ContentValues();
            ReceiverProcessInfo info = mDisableMap.get(key);
            cv.put(ActionTable.PROCESS_NAME, info.processName);
            cv.put(ActionTable.APPNAME, info.appName);
            cv.put(ActionTable.PKG_NAME, info.packageName);
            cv.put(ActionTable.ACATION_TYPE, info.isClose);
            addLists.add(cv);
        }
        if (addLists.size() > 0) {
            ContentValues[] values = new ContentValues[addLists.size()];
            addLists.toArray(values);
            App.mContext.getContentResolver().bulkInsert(
                    ActionTable.CONTENT_URI, values);
            Log.v(TAG,
                    "saveDisableMapForDB_actionMap.size() ==="
                            + mActionMap.size());
        }
    }

    public boolean isAutoStartProcess(
            HashMap<String, ReceiverProcessInfo> autoStartMap,
            String packageName) {
        if (autoStartMap != null && autoStartMap.containsKey(packageName)) {
            return true;
        }
        return false;
    }

    public void saveEnablemapForDB(boolean isRoot,
            HashMap<String, ReceiverProcessInfo> autoStartMap,
            HashMap<String, ReceiverProcessInfo> mEnableMap) {
        ArrayList<String> deleteKeyLists = new ArrayList<String>();
        // enable
        // if (isRoot) {
        // HandleProcess(getAutoStartAppMap(autoStartMap, mEnableMap), true);
        // }

        HashMap<String, ReceiverProcessInfo> curAction = mActionMap;
        Set<String> eSet = mEnableMap.keySet();

        for (String key : eSet) {
            curAction.remove(key);
            deleteKeyLists.add(key);
            Log.v(TAG, "delete_AppName=" + key);
        }
        if (deleteKeyLists.size() > 0) {
            String[] selectionArgs = new String[deleteKeyLists.size()];
            deleteKeyLists.toArray(selectionArgs);
            for (String processname : deleteKeyLists) {
                App.mContext.getContentResolver().delete(
                        ActionTable.CONTENT_URI,
                        ActionTable.PROCESS_NAME + "=?",
                        new String[] { processname });
            }
            Log.v(TAG, "saveEnablemapForDB_actionMap.size() ===" + mActionMap);
        }
    }

    // private void HandleProcess(HashMap<String, ReceiverProcessInfo> map,
    // boolean action) {
    // Iterator<Entry<String, ReceiverProcessInfo>> iter = map.entrySet()
    // .iterator();
    // Log.v(TAG, "map.size()===" + map.size());
    // while (iter.hasNext()) {
    // Map.Entry entry = (Map.Entry) iter.next();
    // ReceiverProcessInfo info = (ReceiverProcessInfo) entry.getValue();
    // Log.v(TAG, "info.reivever===" + info.reivever.get(0));
    // ProcessSettingManager.operatingAplacation(info.reivever, action);
    // }
    // }

    private HashMap<String, ReceiverProcessInfo> getAutoStartAppMap(
            HashMap<String, ReceiverProcessInfo> autoStartMap,
            HashMap<String, ReceiverProcessInfo> anotherMap) {
        HashMap<String, ReceiverProcessInfo> res = new HashMap<String, ReceiverProcessInfo>();
        Set<String> sets = anotherMap.keySet();
        for (String key : sets) {
            if (autoStartMap.containsKey(key)) {
                res.put(key, autoStartMap.get(key));
                Log.v(TAG,
                        "getAutoStartAppMap__"
                                + res.get(key).reivevers.toString());
            }
        }
        return res;
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // TODO Auto-generated method stub
    }
}
